package com.codesuc.aichat.role.controller;

import com.codesuc.aichat.role.entity.Role;
import com.codesuc.aichat.role.service.RoleService;
import com.codesuc.aichat.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @Author: ZhangGongMing
* @CreateTime: 2025/09/23 12:56
* @Description: Role控制层
* @Version: 1.0
*/
@RestController
@RequestMapping(value = "/api/v1/role")
@Tag(name = "RoleController", description = "Role控制层")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有角色列表")
    public Result<java.util.List<Role>> getRoleList(
            @Parameter(description = "角色分类", required = false) @RequestParam(required = false) String category) {
        java.util.List<Role> roleList = roleService.getRoleList(category);
        return Result.ok(roleList);
    }

    /**
     * 分页查询角色列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询角色列表")
    public Result<Page<Role>> getRoleListPage(
            @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") Integer pageNo,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "角色分类", required = false) @RequestParam(required = false) String category) {
        Page<Role> rolePage = roleService.getRoleListPage(pageNo, pageSize, category);
        return Result.ok(rolePage);
    }

    /**
     * 根据ID查询角色详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询角色详情")
    public Result<Role> getRoleById(@Parameter(description = "角色ID", required = true) @PathVariable Long id) {
        Role role = roleService.getById(id);
        if (role != null) {
            return Result.ok(role);
        } else {
            return Result.error("角色不存在");
        }
    }

    /**
     * 新增角色
     */
    @PostMapping("/add")
    @Operation(summary = "新增角色")
    public Result<String> addRole(@RequestBody Role role) {
        boolean success = roleService.save(role);
        if (success) {
            return Result.ok("新增角色成功");
        } else {
            return Result.error("新增角色失败");
        }
    }

    /**
     * 更新角色
     */
    @PutMapping("/update")
    @Operation(summary = "更新角色")
    public Result<String> updateRole(@RequestBody Role role) {
        boolean success = roleService.updateById(role);
        if (success) {
            return Result.ok("更新角色成功");
        } else {
            return Result.error("更新角色失败");
        }
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<String> deleteRole(@Parameter(description = "角色ID", required = true) @PathVariable Long id) {
        boolean success = roleService.removeById(id);
        if (success) {
            return Result.ok("删除角色成功");
        } else {
            return Result.error("删除角色失败");
        }
    }

}
